import pytesseract
from PIL import Image
import speech_recognition as sr
from argostranslate import translate



def image_text_recognition(image_path):
    try:
        img = Image.open(image_path)
        text = pytesseract.image_to_string(img)
        return text
    except Exception as e:
        print(f"图文识别出错: {e}")

def voice_to_text(audio_file):
    r = sr.Recognizer()
    try:
        with sr.AudioFile(audio_file) as source:
            audio = r.record(source)
            text = r.recognize_google(audio)
            return text
    except Exception as e:
        print(f"语音识别出错: {e}")

def translate_text(text, dest_lang):
    translator = translate()
    try:
        translation = translator.translate(text, dest_lang=dest_lang)
        return translation.text
    except Exception as e:
        print(f"翻译出错: {e}")